<template>
  <div class="container">
    <el-row class="header">
      <el-col class="asd" :span="18">
        <popup-tree-input
          class="orgList"
          :data="store.state.system.orgList"
          :prop="data.params.orgname"
          :current-change-handle="handleOrgList"
        />
        <el-input
          class="title"
          clearable
          v-model="data.params.username"
          placeholder="姓名"
          style="width: 120px; margin-right: 15px"
        />
        <el-select clearable v-model="data.params.gender" placeholder="性别">
          <el-option
            v-for="item in data.options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <el-select clearable v-model="data.params.selectOrgs" placeholder="职务">
          <el-option
            v-for="item in ([] as any)"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <el-button class="ordinary" @click="search()">搜索</el-button>
      </el-col>
      <el-col :span="6" align="right">
        <el-button class="ordinary" @click="handleExport()">批量导出</el-button>
        <el-button type="primary" @click="handleAdd()">新增</el-button>
      </el-col>
    </el-row>

    <Table
      :loading="data.isLoading"
      :tableArr="data.allTableData"
      :columns="data.columns"
      @selectList="getCurrentList"
    >
      <template #operate="{ row }">
        <el-button class="ordinary" @click="handleEdit(row)">编辑</el-button>
        <el-button type="danger" @click="handleDel(row)">删除</el-button>
      </template>
    </Table>
    <div class="example-pagination-block">
      <el-pagination
        background
        layout="prev, pager, next ,total,sizes,jumper"
        :total="data.total"
        :page-size="data.params.size"
        v-model:current-page="data.params.current"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
      />
    </div>
    <el-dialog v-model="data.editDialog" width="50%" :title="data.currentHandle">
      <Information :title="data.currentHandle" ref="formRef" v-if="data.editDialog"></Information>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="data.editDialog = false">关闭</el-button>
          <el-button type="primary" @click="handleSave()">保存</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts">
import { reactive, ref } from 'vue'
import tableData from './tableHeader.json'
import Information from './information.vue'
import PopupTreeInput from '@/views/system/components/PopupTreeInput/index.vue'

import { getUserList, saveUser, updataUser, delUser } from '../api/user'
import Table from '../components/table2/table.vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { getOrgTree } from '../api/org'
import { useStore } from 'vuex'

export default {
  components: {
    Table,
    Information,
    PopupTreeInput,
  },
  setup() {
    const store = useStore()
    let data = reactive({
      params: {
        username: '',
        gender: '',
        unitOrg: '',
        orgname: '',
        selectOrgs: '',
        current: 1,
        size: 10,
      },
      total: 0,
      isLoading: false,
      allTableData: [],
      columns: tableData.platformTableHeader,
      ids: [],
      currentList: [],
      currentHandle: '',
      editDialog: false,
      content: {},
      options: [
        {
          value: '0',
          label: '女',
        },
        {
          value: '1',
          label: '男',
        },
      ],
    })
    const orgListProps = reactive({
      checkStrictly: true,
      value: 'id',
      label: 'name',
      emitPath: false,
    })
    const formRef = ref()
    const getCurrentList = (currentList) => {
      console.log(currentList, 'getCurrentList')
      data.currentList = currentList
      data.ids = currentList.map((item) => {
        return item.id
      })
    }
    const search = () => {
      data.params.current = 1
      onSearch()
    }
    const onSearch = async () => {
      data.isLoading = true
      getUserList(data.params).then((res) => {
        data.isLoading = false
        data.allTableData = res.data?.data?.records
        data.allTableData.map((item) => {
          (item as any).birth = (item as any).birth?.slice(0, 10)
        })
        data.total = res.data.data?.total
      })
    }
    onSearch()
    //所属单位修改
    const handleOrgList = (item) => {
      console.log(item, 'handleOrgList')
      data.params.unitOrg = item.id
      data.params.orgname = item.name
    }
    //改变页码
    const handleCurrentChange = (e) => {
      console.log(e, 'handleCurrentChange')
      data.params.current = e
      onSearch()
    }
    //改变页数限制
    const handleSizeChange = (e) => {
      console.log(e, 'handleSizeChange')
      data.params.size = e
      onSearch()
    }

    const handleExport = () => {
      console.log('handleExport')
    }
    const handleAdd = () => {
      data.editDialog = true
      data.currentHandle = '用户新增'
    }
    const handleEdit = (row) => {
      data.editDialog = true
      data.currentHandle = '用户编辑'
      for (let key in store.state.system.userForm) {
        store.state.system.userForm[key] = row[key]
      }
    }
    const handleDel = (row) => {
      ElMessageBox.confirm('您确认删除当前数据吗？', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
        callback: (action) => {
          if (action == 'confirm') {
            console.log(row, 'row')
            delUser(row.userId).then((res) => {
              console.log(res)
              if (res.data.code == 200) {
                onSearch()
                ElMessage({
                  message: '删除成功',
                  type: 'success',
                })
              } else {
                ElMessage({
                  message: '删除失败',
                  type: 'warning',
                })
              }
            })
          }
        },
      })
    }
    const handleSave = () => {
      let params = formRef.value.submitForm()
      if (params) {
        if (data.currentHandle == '用户新增') {
          saveUser(store.state.system.userForm).then((res) => {
            if (res.data.code == 200) {
              data.editDialog = false
              onSearch()
              ElMessage({
                message: '新增成功',
                type: 'success',
              })
            }
          })
        } else if (data.currentHandle == '用户编辑') {
          updataUser(store.state.system.userForm).then((res) => {
            if (res.data.code == 200) {
              data.editDialog = false
              onSearch()
              ElMessage({
                message: '编辑成功',
                type: 'success',
              })
            }
          })
        }
      }
    }
    getOrgTree().then((res) => {
      store.state.system.orgList = res.data.data
    })
    return {
      data,
      store,
      orgListProps,
      formRef,
      onSearch,
      handleOrgList,
      getCurrentList,
      handleCurrentChange,
      handleSizeChange,
      handleEdit,
      handleDel,
      handleAdd,
      handleExport,
      handleSave,
      search,
    }
  },
}
</script>

<style lang="scss" scoped>
.container {
  background-color: white;
  padding: 20px;
}
</style>
